package ch06.d1;

import org.junit.Test;

import java.util.Objects;
import java.util.Random;
import java.util.Scanner;

public class ObjectTest {
    @Test
    public void testObject(){
        Object o1=new Object();
        Object o2=new Object();


        System.out.println(o1==o2);//false
        //equals 比较两个对象是否相等
        System.out.println(o1.equals(o2));//false

        // hashCode 将对象转换成数字
        System.out.println(o1.hashCode());
        System.out.println(o2.hashCode());

        // toString 把对象转换成字符串
        System.out.println(o1);
        System.out.println(o2.toString());

    }

    @Test
    public void testStudent(){
        Student s1=new Student("张三",10);
        Student s2=new Student("张三",11);
        System.out.println(s1.getClass());
//
//        System.out.println(s1.equals(s2));
//
//        System.out.println(s1.hashCode());
//        System.out.println(s2.hashCode());
//
//        System.out.println(s1.toString());
//        System.out.println(s2.toString());

    }

    @Test
    public void testString(){
        String s1=new String("李文斌");
        String s2=new String("李文斌");

//        System.out.println(s1==s2);
        System.out.println(s1.equals(s2));
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());

        System.out.println(s1.toString());
        System.out.println(s2.toString());
    }

    //方法
    public void m1(String s1,String s2) {
//         if(Objects.equals(s1,s2)){
//            //相等
//
//        }else{
//            //不相等
//
//        }

        int x=Objects.hashCode(s1);

        String s=Objects.toString(s1,"默认值");

    }

    @Test
    public void testM1(){
        m1(null,null);
    }


    @Test
    public void testString2(){
        String s1="李文斌";
        String s2="李文斌";
        String s3=new String("李文斌");
        String s4="李";
        String s5=s4+"文斌";

        System.out.println(s1==s2);//true
        System.out.println(s1==s3);//false
        System.out.println(s1==s5);//false


    }

    @Test
    public void testReplaceAll(){
        //正则表达式
        // \d 代表数字
        // \D 代表非数字
        // \w 代表单词字符 [a-zA-Z0-9_]
        // \W 代表非单词字符
        // \s 代表空白字符
        // \S 代表非空白字符
        // . 代表任意字符

        // * 代表任意多个
        // + 代表至少一个
        // ? 代表0个或1个
        // {n} 代表n个
        // {n,m} 代表n到m个

        //[] 代表一个范围 [abc] 代表a或b或c
        String s;
        s="100";
        boolean b1=s.matches("\\d+"); //true

        s="abc";
        boolean b2=s.matches("\\d+"); //false
    }

    @Test
    public void testRandomWord1(){
//        article数组包含冠词：the,a,one,some,any
//        noun数组包含名词boy,girl,dog,town,cat
//        verb数组包含动词drove,jumped,ran,walked,skipped
//        preposition数组应包含介词to,from,over,under,on

        String[] article={"the","a","one","some","any"};
        String[] noun="boy,girl,dog,town,cat,fish".split(",");
        String[] verb="drove,jumped,ran,walked,skipped".split(",");
        String[] preposition="to,from,over,under,on".split(",");
        Random r=new Random();
        String[][] words={article,noun,verb,preposition,article,noun};

        Scanner scan=new Scanner(System.in);

        while(true) {

            String s = "";
            for (int i = 0; i < words.length; i++) {
                int index = r.nextInt(words[i].length);
                s += words[i][index] + " ";
            }

            s=s.substring(0,1).toUpperCase()+s.substring(1);
            System.out.println(s);
            scan.nextLine();


        }

    }


    @Test
    public void testMatches(){
        String s="12611199828qq.com";
        boolean b=s.matches("^[\\w.%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$");
        System.out.println(b);
    }




}
